package com.shiguo365.market.presenter.implement;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.view.IAddressAddView;
import com.shiguo365.market.display.view.IOrderDetailView;
import com.shiguo365.market.presenter.contract.IOrderDetailPresenter;

import java.util.List;

import biz.yfsoft.api.ae.api.AEClient;
import biz.yfsoft.api.ae.api.AEParam;
import biz.yfsoft.api.ae.api.BaseAECallback;

/**
 * Created by caomenglong on 16/8/12.
 */
public class OrderDetailPresenter implements IOrderDetailPresenter {
    private static final String TAG = "OrderDetailPresenter";
    private final Activity mActivity;
    private final IOrderDetailView mOrderDetailView;

    public OrderDetailPresenter(@NonNull Activity activity, @NonNull IOrderDetailView testView) {
        this.mActivity = activity;
        this.mOrderDetailView = testView;
    }

    @Override
    public void getOrderDetail(String orderid) {
        Log.d(TAG,"getOrderDetail===>orderid:"+orderid);
        AEClient _client = new AEClient("shiguo.order.getOrderDetailByOrderid");
        AEParam aeParam=new AEParam();
        aeParam.set("orderid",orderid);
        _client.call(aeParam,new BaseAECallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(@NonNull JSONObject result) {
                Log.d(TAG,result.toJSONString());


                JSONArray jsonObjectList = result.getJSONArray("products");


                Log.d(TAG,jsonObjectList.toJSONString());

                mOrderDetailView.OnGetOrderDetailOK(jsonObjectList);
                mOrderDetailView.OnGetInfoOK(result);
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {
                mOrderDetailView.OnGetOrderDetailFinsh();
            }
        });

    }

    @Override
    public void cancelOrder(String orderid,String status) {

        Log.d(TAG,"现在正在执行撤消订单操作");
        Log.d(TAG,"orderid："+orderid);
        Log.d(TAG,"status："+status);

        AEClient _client = new AEClient("shiguo.order.updateOrderStatus");
        AEParam aeParam=new AEParam();
        aeParam.set("orderid",orderid);
        aeParam.set("status",status);
        _client.call(aeParam,new BaseAECallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(@NonNull JSONObject result) {
                //撤消订单成功
                Log.d(TAG,"撤消订单成功："+result.toString());
                mOrderDetailView.showOrderDetailMessages("恭喜您撤消订单成功,系统正在为你服务");
                mOrderDetailView.orderDetailSuccess();
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {
                Log.e(TAG,"onFinally ......." );
            }
        });

    }
}
